package com.ruoyi.mes.service.impl;

import java.util.*;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mes.mapper.PhgRequirementMapper;
import com.ruoyi.mes.domain.PhgRequirement;
import com.ruoyi.mes.service.IPhgRequirementService;

/**
 * 采购需求Service业务层处理
 * 
 * @author CFL
 * @date 2024-04-28
 */
@Service
public class PhgRequirementServiceImpl implements IPhgRequirementService 
{
    @Autowired
    private PhgRequirementMapper phgRequirementMapper;

    /**
     * 查询采购需求
     * 
     * @param id 采购需求主键
     * @return 采购需求
     */
    @Override
    public PhgRequirement selectPhgRequirementById(Long id)
    {
        return phgRequirementMapper.selectPhgRequirementById(id);
    }

    /**
     * 查询产品物料信息
     *
     * @param proId 采购需求外键
     * @return 采购需求
     */
    @Override
    public List<PhgRequirement> selectPhgRequirementByProId(Long proId)
    {
        return phgRequirementMapper.selectPhgRequirementByProId(proId);
    }

    /**
     * 查询采购需求列表
     * 
     * @param phgRequirement 采购需求
     * @return 采购需求
     */
    @Override
    public List<PhgRequirement> selectPhgRequirementList(PhgRequirement phgRequirement)
    {
        return phgRequirementMapper.selectPhgRequirementList(phgRequirement);
    }

    /**
     * 新增采购需求
     * 
     * @param phgRequirement 采购需求
     * @return 结果
     */
    @Override
    public int insertPhgRequirement(PhgRequirement phgRequirement)
    {
        return phgRequirementMapper.insertPhgRequirement(phgRequirement);
    }

    /**
     * 修改-物料计划新增采购需求
     *
     * @param phgRequirementList 采购需求
     * @return 结果
     */
    @Override
    public AjaxResult insertRequirement(List<PhgRequirement> phgRequirementList)
    {
        BaseController baseController = new BaseController();
        String username = baseController.getUsername();
        Date date = DateUtils.getNowDate();
        Long orderId = phgRequirementList.get(0).getOrderId();
        PhgRequirement phgRequirement1 = phgRequirementMapper.selectPhgRequirementByOrderId(orderId);
        if (phgRequirement1 != null) {
            return AjaxResult.error("请勿重复下达采购需求");
        }
        for (PhgRequirement phgRequirement : phgRequirementList){
            phgRequirement.setCreatedBy(username);
            phgRequirement.setCreatedTime(date);
            phgRequirement.setState(0);
        }
        phgRequirementMapper.insertRequirement(phgRequirementList);
//        phgRequirementDetailMapper.insertPhgRequirementDetail(phgRequirementList);
        return AjaxResult.success("采购需求下达成功");
    }

    /**
     * 修改采购需求
     * 
     * @param phgRequirement 采购需求
     * @return 结果
     */
    @Override
    public int updatePhgRequirement(PhgRequirement phgRequirement)
    {
        return phgRequirementMapper.updatePhgRequirement(phgRequirement);
    }

    /**
     * 批量删除采购需求
     * 
     * @param ids 需要删除的采购需求主键
     * @return 结果
     */
    @Override
    public int deletePhgRequirementByIds(Long[] ids)
    {
        return phgRequirementMapper.deletePhgRequirementByIds(ids);
    }

    /**
     * 删除采购需求信息
     * 
     * @param id 采购需求主键
     * @return 结果
     */
    @Override
    public int deletePhgRequirementById(Long id)
    {
        return phgRequirementMapper.deletePhgRequirementById(id);
    }
}
